

#include "HT162X.h"
#include "HT162X_Parts.h"
#include "HT162X_Parts_Num.h"
#include "UI.h"
#include "../MCU_Mold/MCU_Time.h"
#include "../MCU_Mold/MCU_AbstractDigitron.h"


void Lcd_Set(Char_E dis)
{
    Cad_Dat.Cad_Com = Cad_Com1;
    Cad_Dat.Cad_Pin = Cad_Pin_10;
    Cad_Dat.Cad_Dis = dis; 

    HT_RAM_Setting(Cad_Dat);
}
void Lcd_Motor(Char_E dis)
{
    Cad_Dat.Cad_Com = Cad_Com1;
    Cad_Dat.Cad_Pin = Cad_Pin_14;
    Cad_Dat.Cad_Dis = dis; 

    HT_RAM_Setting(Cad_Dat);
}
void Lcd_Fan(Char_E dis)
{
    Cad_Dat.Cad_Com = Cad_Com1;
    Cad_Dat.Cad_Pin = Cad_Pin_12;
    Cad_Dat.Cad_Dis = dis; 

    HT_RAM_Setting(Cad_Dat);
}
void Lcd_HotRod(Char_E dis)
{
    Cad_Dat.Cad_Com = Cad_Com1;
    Cad_Dat.Cad_Pin = Cad_Pin_27;
    Cad_Dat.Cad_Dis = dis; 

    HT_RAM_Setting(Cad_Dat);
}
void Lcd_Wifi(Char_E dis)
{
    Cad_Dat.Cad_Com = Cad_Com2; //S4
    Cad_Dat.Cad_Pin = Cad_Pin_28;
    Cad_Dat.Cad_Dis = dis; 

    HT_RAM_Setting(Cad_Dat);

    Cad_Dat.Cad_Com = Cad_Com2; //S5
    Cad_Dat.Cad_Pin = Cad_Pin_27;
    Cad_Dat.Cad_Dis = dis; 

    HT_RAM_Setting(Cad_Dat);

    Cad_Dat.Cad_Com = Cad_Com3; //S6
    Cad_Dat.Cad_Pin = Cad_Pin_27;
    Cad_Dat.Cad_Dis = dis; 

    HT_RAM_Setting(Cad_Dat);
}
void Lcd_Blue(Char_E dis)
{
    Cad_Dat.Cad_Com = Cad_Com3;
    Cad_Dat.Cad_Pin = Cad_Pin_28;
    Cad_Dat.Cad_Dis = dis; 

    HT_RAM_Setting(Cad_Dat);
}
void Lcd_Timer_Title(Char_E dis)
{
    Cad_Dat.Cad_Com = Cad_Com5;
    Cad_Dat.Cad_Pin = Cad_Pin_20;
    Cad_Dat.Cad_Dis = dis; 

    HT_RAM_Setting(Cad_Dat);
}
void Lcd_Timer_Colon(Char_E dis)
{
    Cad_Dat.Cad_Com = Cad_Com5;
    Cad_Dat.Cad_Pin = Cad_Pin_22;
    Cad_Dat.Cad_Dis = dis; 

    HT_RAM_Setting(Cad_Dat);
}
void Lcd_P1(Char_E dis)
{
    Cad_Dat.Cad_Com = Cad_Com4;
    Cad_Dat.Cad_Pin = Cad_Pin_22;
    Cad_Dat.Cad_Dis = dis; 

    HT_RAM_Setting(Cad_Dat); 
}
void Lcd_P2(Char_E dis)
{
    Cad_Dat.Cad_Com = Cad_Com4;
    Cad_Dat.Cad_Pin = Cad_Pin_18;
    Cad_Dat.Cad_Dis = dis; 

    HT_RAM_Setting(Cad_Dat);  
}
void Lcd_Error(Char_E dis)
{
    Cad_Dat.Cad_Com = Cad_Com5;
    Cad_Dat.Cad_Pin = Cad_Pin_14;
    Cad_Dat.Cad_Dis = dis; 

    HT_RAM_Setting(Cad_Dat); 
}
void Lcd_Cell(Char_E dis)
{
    Cad_Dat.Cad_Com = Cad_Com4; //X1
    Cad_Dat.Cad_Pin = Cad_Pin_27;
    Cad_Dat.Cad_Dis = dis; 
    HT_RAM_Setting(Cad_Dat); 

    Cad_Dat.Cad_Com = Cad_Com4; //X2
    Cad_Dat.Cad_Pin = Cad_Pin_28;
    Cad_Dat.Cad_Dis = dis; 
    HT_RAM_Setting(Cad_Dat); 

    Cad_Dat.Cad_Com = Cad_Com5; //X3
    Cad_Dat.Cad_Pin = Cad_Pin_27;
    Cad_Dat.Cad_Dis = dis; 
    HT_RAM_Setting(Cad_Dat); 

    Cad_Dat.Cad_Com = Cad_Com5; //X4
    Cad_Dat.Cad_Pin = Cad_Pin_28;
    Cad_Dat.Cad_Dis = dis; 
    HT_RAM_Setting(Cad_Dat); 
}
void Lcd_Act(Char_E dis)
{
    Cad_Dat.Cad_Com = Cad_Com5;
    Cad_Dat.Cad_Pin = Cad_Pin_12;
    Cad_Dat.Cad_Dis = dis; 

    HT_RAM_Setting(Cad_Dat); 
}
void Lcd_Fire_Animation(uint16_t param)
{
    uint8_t i;
    uint8_t x;

    const uint8_t temp_Com[10] = {Cad_Com5,Cad_Com5,Cad_Com5,Cad_Com5,Cad_Com6,Cad_Com6,Cad_Com7,Cad_Com7,Cad_Com8,Cad_Com8,}; //Fire: S11 ~ S20
    const uint8_t temp_Pin[10] = {Cad_Pin_26,Cad_Pin_15,Cad_Pin_17,Cad_Pin_18,Cad_Pin_17,Cad_Pin_18,Cad_Pin_17,Cad_Pin_18,Cad_Pin_17,Cad_Pin_18,};

    if(UI_System.Grill_Unit == Unit_F) x = param / (Oven_Setting_Max_F/10);
    else x = param / (Oven_Setting_Max_C/10);
    if(x < 1) x = 1;
    for (i = 0; i < 10; i++)
    {
        Cad_Dat.Cad_Com = temp_Com[i];
        Cad_Dat.Cad_Pin = temp_Pin[i];
        if(i < x) Cad_Dat.Cad_Dis = Char_ON;
        else Cad_Dat.Cad_Dis = Char_OFF;
        HT_RAM_Setting(Cad_Dat);
    }
}
void Lcd_Fire(Char_E dis)
{
    uint8_t i;
    const uint8_t temp_Com[10] = {Cad_Com5,Cad_Com5,Cad_Com5,Cad_Com5,Cad_Com6,Cad_Com6,Cad_Com7,Cad_Com7,Cad_Com8,Cad_Com8,}; //Fire: S11 ~ S20
    const uint8_t temp_Pin[10] = {Cad_Pin_26,Cad_Pin_15,Cad_Pin_17,Cad_Pin_18,Cad_Pin_17,Cad_Pin_18,Cad_Pin_17,Cad_Pin_18,Cad_Pin_17,Cad_Pin_18,};

    for (i = 0; i < 10; i++)
    {
        Cad_Dat.Cad_Com = temp_Com[i];
        Cad_Dat.Cad_Pin = temp_Pin[i];
        Cad_Dat.Cad_Dis = dis; 
        HT_RAM_Setting(Cad_Dat);
    }
}
void Lcd_Line(Char_E dis)
{
    Cad_Dat.Cad_Com = Cad_Com5; //S21
    Cad_Dat.Cad_Pin = Cad_Pin_24;
    Cad_Dat.Cad_Dis = dis; 
    HT_RAM_Setting(Cad_Dat); 

    Cad_Dat.Cad_Com = Cad_Com4; //S23
    Cad_Dat.Cad_Pin = Cad_Pin_20;
    Cad_Dat.Cad_Dis = dis; 
    HT_RAM_Setting(Cad_Dat); 
}

void Lcd_Parts_Show(void)
{
    Lcd_Set(Char_ON);
    Lcd_Motor(Char_ON);
    Lcd_Fan(Char_ON);
    Lcd_HotRod(Char_ON);
    Lcd_Wifi(Char_ON);
    Lcd_Blue(Char_ON);
    Lcd_Timer_Title(Char_ON);
    Lcd_Timer_Colon(Char_ON);
    Lcd_P1(Char_ON);
    Lcd_P2(Char_ON);
    Lcd_Error(Char_ON);
    Lcd_Cell(Char_ON);
    Lcd_Act(Char_ON);
    Lcd_Fire(Char_ON);
    Lcd_Line(Char_ON);

    Lcd_NumShow(UI_RTDSettings,888);
    Lcd_NumShow(UI_RTD,888);
    Lcd_NumShow(UI_Timer_Hour,88);
    Lcd_NumShow(UI_Timer_Minute,88);
    Lcd_NumShow(UI_ProbeA,888);
    Lcd_NumShow(UI_ProbeB,888);
}
void Lcd_Parts_NotShow(void)
{
    Lcd_Set(Char_OFF);
    Lcd_NumShow(UI_RTDSettings,888);
    Lcd_Act(Char_OFF);
    Lcd_Fire(Char_OFF);
    Lcd_NumShow(UI_RTD,888);
    Lcd_P1(Char_OFF);
    Lcd_P2(Char_OFF);
    Lcd_NumShow(UI_ProbeA,888);
    Lcd_NumShow(UI_ProbeB,888);
    Lcd_Line(Char_OFF);
}

void Lcd_Parts_Refresh(void)
{
    MCU_Time_T t = {0,0,0};
    uint16_t temp = 0;

    Lcd_Motor(UI_System.ICO_Auger);
    Lcd_Fan(UI_System.ICO_Fan);
    Lcd_HotRod(UI_System.ICO_Heating);
    Lcd_Error(UI_System.ICO_Error);
    Lcd_Cell(UI_System.ICO_Cell);
    Lcd_Blue(UI_System.ICO_Bluetooth);

    if(UI_System.ICO_WIFI == UI_OFF)
    {
        if(UI_System.Shine_WIFI == Shine_ON) Lcd_Wifi(UI_ON);
        else Lcd_Wifi(UI_OFF);
    }
    else Lcd_Wifi(UI_ON);

    if(UI_System.State_Operation == UI_None)
    {
        TotalConvertTime(&t,UI_System.Timer1.Actual);
        Lcd_NumShow(UI_Timer_Hour,t.hour);
        if(UI_System.Timer1.Actual < 60)
        { //seconds的显示位跳转到min的显示位
            Lcd_NumShow(UI_Timer_Minute,UI_System.Timer1.Actual);
        }
        else Lcd_NumShow(UI_Timer_Minute,t.minute);
        if(UI_System.Grill_state != State_Grill_Misfire)
        {
            Lcd_NumShow(UI_RTD,UI_System.RTD.Actual);
            Lcd_NumShow(UI_RTDSettings,UI_System.RTD.Settings);

            Lcd_NumShow(UI_ProbeA,UI_System.ProbeA.Actual);
            Lcd_NumShow(UI_ProbeB,UI_System.ProbeB.Actual);
        }
        else
        { //熄火状态
            Lcd_Parts_NotShow();
        }

        if(UI_System.Timer1.Actual > 0)
        {
            if(UI_System.Shine_Colon == Shine_ON) Lcd_Timer_Colon(Char_ON);
            else Lcd_Timer_Colon(Char_OFF);
        }
        else Lcd_Timer_Colon(Char_ON);
    }
    else
    {
        Lcd_NumShow_Shine(UI_RTD,UI_System.RTD.Settings);

        TotalConvertTime(&t,UI_System.Timer1.Settings);
        Lcd_NumShow_Shine(UI_Timer_Hour,t.hour);
        Lcd_NumShow_Shine(UI_Timer_Minute,t.minute);
        Lcd_Timer_Colon(Char_ON); //操作模式一直显示冒号
        if(UI_System.ProbeA.Settings == 0)
        {
            Lcd_Number(Nixie_NumEncoder_Crossbar,P1_seg1);
            Lcd_Number(Nixie_NumEncoder_Crossbar,P1_seg2);
            Lcd_Number(Nixie_NumEncoder_Crossbar,P1_seg3); 
        }
        else Lcd_NumShow_Shine(UI_ProbeA, UI_System.ProbeA.Settings);
        if(UI_System.ProbeB.Settings == 0)
        {
            Lcd_Number(Nixie_NumEncoder_Crossbar,P2_seg1);
            Lcd_Number(Nixie_NumEncoder_Crossbar,P2_seg2);
            Lcd_Number(Nixie_NumEncoder_Crossbar,P2_seg3);
        }
        else Lcd_NumShow_Shine(UI_ProbeB, UI_System.ProbeB.Settings);
    }
    
    if(UI_Grill_Normal() == UI_ON)
    {
        if(UI_System.State_Operation != UI_None) temp = UI_System.RTD.Settings;
        else temp = UI_System.RTD.Actual;
        Lcd_Fire_Animation(temp);
    }
    else Lcd_Fire(Char_OFF);
}
